#include <iostream>
#include <string>
#include <unistd.h>
#include <vector>
#include <pthread.h>

using namespace std;

pthread_mutex_t gmutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t gcond = PTHREAD_COND_INITIALIZER;
void *RunAtest(void* args)
{
    string name = static_cast<char*>(args);
    while(true)
    {
        sleep(1);  
        cout << "Thread-A: 我是线程A" << endl;
        pthread_cond_signal(&gcond);//唤醒
    }
    return nullptr;
}
void *RunBtest(void* args)
{
    string name = static_cast<char*>(args);
    while(true)
    {
        // pthread_mutex_lock(&gmutex);//加锁
        pthread_cond_wait(&gcond, &gmutex);//等待
        cout << "Thread-B: 我是线程B" << endl;
       
        // pthread_mutex_unlock(&gmutex);//解锁
    }
    return nullptr;
}
void StatrtAthread(vector<pthread_t> *tidptr)
{
    pthread_t tid;
    int n = pthread_create(&tid, nullptr, RunAtest, (void*)"Thread-A");
    if(n == 0)
    {
        cout << "Thread-A create success" << endl;
    }
    tidptr->emplace_back(tid);
}
void StatrtBthread(vector<pthread_t> *tidptr)
{
    pthread_t tid;
    int n = pthread_create(&tid, nullptr, RunBtest, (void*)"Thread-B");
    if(n == 0)
    {
        cout << "Thread-B create success" << endl;
    }
    tidptr->emplace_back(tid);
}
void WaitThreads(vector<pthread_t> &tidptr)
{
    for(auto& thread:tidptr)
    {
        pthread_join(thread, nullptr);
    }
}
int main()
{
    vector<pthread_t> tids;
    StatrtAthread(&tids);
    StatrtBthread(&tids);
    WaitThreads(tids);
    return 0;
}